package service;

import dao.CustomerDao;
import model.Customer;
import java.util.List;
import java.util.Scanner;

public class CustomerService {
    private CustomerDao customerDao;
    private Scanner scanner;

    public CustomerService() {
        this.customerDao = new CustomerDao();
        this.scanner = new Scanner(System.in);
    }

    public void addCustomer() {
        System.out.println("\n=== 添加客户 ===");
        System.out.print("请输入客户ID: ");
        String id = scanner.nextLine();

        if (customerDao.findCustomerById(id) != null) {
            System.out.println("客户ID已存在！");
            return;
        }

        System.out.print("请输入客户姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入客户类型 (1-个人, 2-企业): ");
        String typeInput = scanner.nextLine();
        String type = typeInput.equals("2") ? "企业" : "个人";

        System.out.print("请输入电话号码: ");
        String phone = scanner.nextLine();
        System.out.print("请输入邮箱: ");
        String email = scanner.nextLine();
        System.out.print("请输入地址: ");
        String address = scanner.nextLine();

        System.out.print("请输入VIP等级 (1-普通, 2-银卡, 3-金卡, 4-钻石): ");
        String vipInput = scanner.nextLine();
        String vipLevel = "普通";
        switch (vipInput) {
            case "2": vipLevel = "银卡"; break;
            case "3": vipLevel = "金卡"; break;
            case "4": vipLevel = "钻石"; break;
        }

        Customer customer = new Customer(id, name, phone, email);

        customerDao.addCustomer(customer);
        System.out.println("客户添加成功！");
    }

    public void viewAllCustomers() {
        System.out.println("\n=== 所有客户信息 ===");
        List<Customer> customers = customerDao.getAllCustomers();

        if (customers.isEmpty()) {
            System.out.println("暂无客户信息！");
            return;
        }

        System.out.println("ID\t姓名\t类型\t电话\t\t邮箱");
        System.out.println("--------------------------------------------------------");
        for (Customer customer : customers) {
            System.out.printf("%s\t%s\t%s\t%s\t%s\n",
                    customer.getId(), customer.getName(),
                    "个人", // 这里可以扩展类型
                    customer.getPhone(), customer.getEmail());
        }
    }

    public Customer findCustomerById(String id) {
        return customerDao.findCustomerById(id);
    }

    // 搜索客户方法
    public void searchCustomer(String keyword) {
        System.out.println("\n=== 搜索客户结果 ===");
        List<Customer> customers = customerDao.getAllCustomers();
        boolean found = false;

        for (Customer customer : customers) {
            if (customer.getId().contains(keyword) || customer.getName().contains(keyword)) {
                if (!found) {
                    System.out.println("ID\t姓名\t电话\t\t邮箱");
                    System.out.println("----------------------------------------");
                    found = true;
                }
                System.out.printf("%s\t%s\t%s\t%s\n",
                        customer.getId(), customer.getName(),
                        customer.getPhone(), customer.getEmail());
            }
        }

        if (!found) {
            System.out.println("未找到匹配的客户！");
        }
    }

    // 删除客户方法
    public void deleteCustomer(String id) {
        Customer customer = customerDao.findCustomerById(id);
        if (customer == null) {
            System.out.println("客户不存在！");
            return;
        }

        customerDao.deleteCustomer(id);
        System.out.println("客户删除成功！");
    }
}